//
//  ESTActivityListViewController.m
//  ESTSCRMAPP
//
//  Created by 李贤立 on 2018/10/16.
//  Copyright © 2018年 Capfer. All rights reserved.
//

#import "ESTActivityListViewController.h"
#import "ESTActivityEffectiveView.h"
#import "ESTActivityModel.h"
#import "ESTActivityListTableViewCell.h"
#import "ESTActivityDetailViewController.h"

@interface ESTActivityListViewController ()<UITableViewDelegate, UITableViewDataSource>
/**
 *  是否有效活动
 */
@property(nonatomic, assign) BOOL effective;
/**
 *  TableView
 */
@property(nonatomic, strong) TYZTableView *tableView;
/**
 *  数据源
 */
@property(nonatomic, strong) NSMutableArray<ESTActivityModel *> *modelArray;
/**
 *  请求的页码
 */
@property(nonatomic, assign) NSInteger page_num;
/**
 *  总共多少个数据
 */
@property(nonatomic, assign) NSInteger dataNumber;
/**
 *  是否删除了TableView
 */
@property(nonatomic, assign) BOOL isNoData;
@end

@implementation ESTActivityListViewController
#pragma mark - 生命周期方法
- (void)viewDidLoad {
    [super viewDidLoad];
    self.mainTitle = [@"营销活动" S];
    [self setupActivityEffectiveHeader];
    [self setupTableView];
    self.effective = YES;
}
- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
}
- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
    [self pullDownRefreshData];
}
#pragma mark - 设置UI
- (void)setupTableView {
    [self.view addSubview:self.tableView];
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.bottom.equalTo(self.view);
        make.top.equalTo(self.view).offset(40);
    }];
    self.tableView.estimatedRowHeight = 100;
    [self setupTableViewHeader];
}
- (void)setupTableViewHeader {
    WEAKSELF
    self.tableView.mj_header = [MJRefreshStateHeader headerWithRefreshingBlock:^{
        [weakSelf pullDownRefreshData];
    }];
}
- (void)setupTableViewFooter {
    WEAKSELF
    self.tableView.mj_footer = [MJRefreshAutoStateFooter footerWithRefreshingBlock:^{
        [weakSelf pullUpLoadMore];
    }];
}
#pragma mark - actions
- (void)acquireData {
    WEAKSELF
    [TYZLoadingView showMessage:nil];
    [ESTActivityModel requestAllActivityWithIS_ENABLE:self.effective page_num:self.page_num success:^(NSArray<ESTActivityModel *> *modelArray) {
        [TYZLoadingView hideHUD];
        [weakSelf handleModelArray:modelArray];
        [weakSelf.tableView.mj_header endRefreshing];
    } failure:^(NSString *error) {
        [TYZLoadingView showError:NetworkUnusual];
        [weakSelf.tableView.mj_header endRefreshing];
        [weakSelf handleModelArray:nil];
    }];
}
- (void)handleModelArray:(NSArray<ESTActivityModel *> *)modelArray {
    if (modelArray.count > 0) {
        [self.tableView.mj_footer endRefreshing];
        [self.modelArray addObjectsFromArray:modelArray];
        if (modelArray.count < Request_PageSize_Activity) {
            self.page_num--;
            [self.tableView.mj_footer endRefreshingWithNoMoreData];
        }
    } else {
        [self.tableView.mj_footer endRefreshingWithNoMoreData];
    }
    if (self.modelArray.count > 0) {
        [self.view removeNoDataImage];
        if (self.isNoData) {
            self.isNoData = NO;
            [self setupTableView];
        } else {
            [self.tableView reloadData];
        }
        if (self.modelArray.count < Request_PageSize_Activity) {
            [self.tableView.mj_footer removeFromSuperview];
        } else {
            if (self.tableView.mj_footer == nil) {
                [self setupTableViewFooter];
            }
        }
    } else {
        self.isNoData = YES;
        [self.tableView removeFromSuperview];
        WEAKSELF
        [self.view noDataWithClick:^{
            [weakSelf pullDownRefreshData];
        }];
    }
}
- (void)pullDownRefreshData {
    self.page_num = 1;
    [self.modelArray removeAllObjects];
    [self acquireData];
}
- (void)pullUpLoadMore {
    self.page_num++;
    [self acquireData];
}
- (void)setupActivityEffectiveHeader {
    ESTActivityEffectiveView *leftView = [ESTActivityEffectiveView creatActivityEffectiveViewWithTitle:[@"进行中" S]];
    [self.view addSubview:leftView];
    [leftView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.equalTo(self.view);
        make.height.mas_equalTo(40);
        make.right.equalTo(self.view.mas_centerX);
    }];
    leftView.isSelected = YES;
    ESTActivityEffectiveView *rightView = [ESTActivityEffectiveView creatActivityEffectiveViewWithTitle:[@"已结束" S]];
    [self.view addSubview:rightView];
    [rightView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.top.equalTo(self.view);
        make.height.mas_equalTo(40);
        make.left.equalTo(self.view.mas_centerX);
    }];
    WEAKSELF
    __weak typeof(leftView) weakLeftView = leftView;
    __weak typeof(rightView) weakRightView = rightView;
    [leftView setDidCliclCoverBlock:^{
        weakSelf.effective = YES;
        weakLeftView.isSelected = YES;
        weakRightView.isSelected = NO;
        [weakSelf pullDownRefreshData];
    }];
    [rightView setDidCliclCoverBlock:^{
        weakSelf.effective = NO;
        weakLeftView.isSelected = NO;
        weakRightView.isSelected = YES;
        [weakSelf pullDownRefreshData];
    }];
}
#pragma mark - UITableViewDelegate, UITableViewDataSource
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return Value_Height_PartPadding;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    TYZView *view = [[TYZView alloc] init];
    view.backgroundColor = ESTBackgroundColor;
    return view;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return self.modelArray.count;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 1;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return UITableViewAutomaticDimension;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    ESTActivityListTableViewCell *cell = [ESTActivityListTableViewCell creatActivityListTableViewCell:tableView];
    cell.model = self.modelArray[indexPath.section];
    WEAKSELF
    [cell setDidClickSpeakBlock:^{
        ESTActivityDetailViewController *controller = [[ESTActivityDetailViewController alloc] init];
        controller.activity_id = weakSelf.modelArray[indexPath.section].ACTIVITY_ID;
        controller.isSpaekDetail = YES;
        [weakSelf.navigationController pushViewController:controller animated:YES];
    }];
    return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    ESTActivityDetailViewController *controller = [[ESTActivityDetailViewController alloc] init];
    controller.activity_id = self.modelArray[indexPath.section].ACTIVITY_ID;
    [self.navigationController pushViewController:controller animated:YES];
}
#pragma mark - setter & getter
- (TYZTableView *)tableView {
    if (_tableView == nil) {
        _tableView = [[TYZTableView alloc] init];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    }
    return _tableView;
}
- (NSMutableArray<ESTActivityModel *> *)modelArray {
    if (_modelArray == nil) {
        _modelArray = [NSMutableArray array];
    }
    return _modelArray;
}
@end
